package com.ihg.hiex.admin.service.account.impl;

import java.util.List;

import org.springframework.stereotype.Service;

import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.entity.page.Page;
import com.demo2do.core.persistence.annotation.Dao;
import com.ihg.hiex.admin.dao.account.AccountDao;
import com.ihg.hiex.admin.service.DataServiceImpl;
import com.ihg.hiex.admin.service.account.AccountService;
import com.ihg.hiex.entity.user.Account;

/**
 * @author houtao
 * 
 */
@Service("accountService")
public class AccountServiceImpl extends DataServiceImpl<Account> implements AccountService {

    @Dao
    private AccountDao accountDao;
    
    /* (non-Javadoc)
     * @see com.ihg.hiex.admin.service.account.AccountService#count(java.lang.String)
     */
    public int count(String authority) {
        return accountDao.count(authority);
    }
    
    /* (non-Javadoc)
     * @see com.ihg.hiex.admin.service.account.AccountService#list(java.lang.String, com.demo2do.core.entity.page.Page)
     */
    public List<Account> list(String authority, Page page) {
        List<Account> result = accountDao.list(authority, page.getBeginIndex(), page.getEveryPage());
        page.calculate();
        return result;
    }
    
    /*
     * (non-Javadoc)
     * @see com.ihg.hiex.admin.service.account.AccountService#list(java.lang.String)
     */
    public List<Account> list(String authority) {
        return accountDao.list(authority);
    }
    
    /* (non-Javadoc)
     * @see com.ihg.hiex.admin.service.account.AccountService#loadByName(java.lang.String)
     */
    public Account loadByName(String name) {
        return accountDao.loadByEmail(name);
    }
    
    /* (non-Javadoc)
     * @see com.ihg.hiex.admin.service.account.AccountService#editPassword(com.ihg.hiex.entity.user.Account, java.lang.String, java.lang.String)
     */
    public void editPassword(Account account, String oldPassword, String newPassword) {
        
        boolean validOldPassword = account.validPassword(oldPassword);
        if (!validOldPassword) {
            throw BusinessException.newInstance("500", "Old Password Incorrect");
        }
        
        genericDaoSupport.update(account.editPassword(newPassword));
        
    }

    /* (non-Javadoc)
     * @see com.ihg.hiex.admin.service.account.AccountService#disable(com.ihg.hiex.entity.user.Account)
     */
    public void disable(Account account) {
        genericDaoSupport.update(account.disable());
    }

}